﻿# coding: utf-8

import os
import wx
import wx.lib.agw.floatspin as FS

from wxFramework.Dialog import wxDialogLogicGridCreate

BRUSH_SIZE = 64

class wxPanelLogicGrid(wx.Panel):
	def __init__(self, parent, id):
		wx.Panel.__init__(self, parent, id)
		
		# Layout
		sizerGenerate = self.__CreateUIGenerate()
		sizerBrushParam = self.__CreateUIBrushParam()
		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(sizerGenerate, 0, wx.ALL|wx.EXPAND, border = 10)
		sizer.Add(sizerBrushParam, 0, wx.ALL|wx.EXPAND, border = 10)
		self.SetSizer(sizer)
		
		# Event bind
		self.Bind(wx.EVT_BUTTON, self.__OnBnClickGenerate, self._buttonGenerate)
		self.Bind(wx.EVT_BUTTON, self.__OnBnClickLoad, self._buttonLoad)
		self.Bind(wx.EVT_BUTTON, self.__OnBnClickSave, self._buttonSave)
		self.Bind(wx.EVT_CHECKBOX, self.__OnCheckBoxDisplay, self._ckDisplay)
		self._fsBrushSize.Bind(FS.EVT_FLOATSPIN, self.__OnFloatSpinBrushSize)
		
	def __OnFloatSpinBrushSize(self, e):
		if not LOGIC_GRID_EDITOR.IsEditing():
		    return
		    
		size = self._fsBrushSize.GetValue()
		LOGIC_GRID_EDITOR.SetBrushSize(size)
	    
	def __CreateUIGenerate(self):
		""" Create generate UI """
		
		# Display switch
		self._ckDisplay = wx.CheckBox(self, -1, TD.LOGIC_GRID_DISPLAY_SWITCH)
		
		# Create, load and save logic grid.
		self._buttonGenerate = wx.Button(self, -1, TD.LOGIC_GRID_GENERATE)
		self._buttonLoad = wx.Button(self, -1, TD.LOGIC_GRID_LOAD)
		self._buttonSave = wx.Button(self, -1, TD.LOGIC_GRID_SAVE)
		sizer = wx.BoxSizer(wx.HORIZONTAL)
		sizer.Add(self._buttonGenerate, 0, wx.ALL, border = 5)
		sizer.Add(self._buttonLoad, 0, wx.ALL, border = 5)
		sizer.Add(self._buttonSave, 0, wx.ALL, border = 5)
		
		sizerGenerate = wx.BoxSizer(wx.VERTICAL)
		sizerGenerate.Add(self._ckDisplay, 0, wx.ALL|wx.EXPAND, border = 5)
		sizerGenerate.Add(sizer, 0, wx.ALL|wx.EXPAND, border = 5)
		return sizerGenerate
		
	def __CreateUIBrushParam(self):
		""" Create brush parameter UI """
		
		# Brush size
		staticBrushSize = wx.StaticText(self, -1, TD.LOGIC_GRID_BRUSH_SIZE)
		self._fsBrushSize = FS.FloatSpin(self, -1, min_val = 0, max_val = 20, increment = 1, agwStyle = FS.FS_RIGHT)
		self._fsBrushSize.SetDigits(0)
		self._fsBrushSize.SetFormat('%f')
		sizerBrushSize = wx.BoxSizer(wx.HORIZONTAL)
		sizerBrushSize.Add(staticBrushSize, 0, wx.CENTER, border = 5)
		sizerBrushSize.Add(self._fsBrushSize, 1, wx.LEFT|wx.RIGHT|wx.EXPAND, border = 5)
		
		sizerBrushParam = wx.StaticBoxSizer(wx.StaticBox(self, -1, TD.LOGIC_GRID_BRUSH_PARAM), wx.VERTICAL)
		sizerBrushParam.Add(sizerBrushSize, 0, wx.ALL|wx.EXPAND, border = 5)
		return sizerBrushParam
		
	def __OnBnClickGenerate(self, e):
		dlg = wxDialogLogicGridCreate(self)
		if dlg.ShowModal() == wx.ID_OK:
			try:
				width = dlg.GetWidth()
				height = dlg.GetHeight()
				gridSize = dlg.GetGridSize()
			except:
				dlgMsg = wx.MessageDialog(self, TD.ERR_PARAM_INVALID, TD.ERROR, wx.OK|wx.ICON_INFORMATION)
				dlgMsg.ShowModal()
				dlgMsg.Destroy()
				dlg.Destroy()
				return
					
			if not LOGIC_GRID_EDITOR.CreateGrid(width, height, gridSize):
				dlgMsg = wx.MessageDialog(self, TD.ERR_CREATE_LOGIC_GRID_FAILED, TD.ERROR, wx.OK|wx.ICON_INFORMATION)
				dlgMsg.ShowModal()
				dlgMsg.Destroy()
						
		dlg.Destroy()
	
	def __OnBnClickLoad(self, e):
		pass
		
	def __OnBnClickSave(self, e):
		if not LOGIC_GRID_EDITOR.IsEditing():
			return

		dlg = wx.FileDialog(self, defaultDir = os.getcwd(), 
			wildcard = "LogicGrid File (*.lgd)|*.lgd|All files (*.*)|*.*", style=wx.SAVE)
		
		if dlg.ShowModal() == wx.ID_OK:
			path = dlg.GetPath().encode('gbk')
			
			if not LOGIC_GRID_EDITOR.SaveToFile(path):
				dlgMsg = wx.MessageDialog(self, TD.ERR_SAVE_LOGIC_GRID_FAILED, TD.ERROR, wx.OK|wx.ICON_INFORMATION)
				dlgMsg.ShowModal()
				dlgMsg.Destroy()
			
		dlg.Destroy()		
		
	def __OnCheckBoxDisplay(self, e):
		if e.IsChecked():
			WORLD_EDITOR.ChangeCurrentOperate(OPD.OP_LOGIC_GRID_PAINT)
			LOGIC_GRID_EDITOR.SetVisable(True)
		else:
			WORLD_EDITOR.ChangeCurrentOperate(OPD.OP_SELECT)
			LOGIC_GRID_EDITOR.SetVisable(False)
		